package org.sky.algorithm.linear;

import java.util.Arrays;

/**
 * 栈: 先进后出
 *
 * @author fog
 * @date 2021/7/28
 */

public class MyStack {

    private int[] elements;

    public MyStack() {
        elements = new int[0];
    }

    public void show() {
        System.out.println(Arrays.toString(elements));
    }

    /**
     * 入栈
     *
     * @param element 入栈元素
     * @author fog
     */
    public void push(int element) {

        int[] newList = new int[elements.length + 1];

        for (int i = 0; i < elements.length; i++) {
            newList[i] = elements[i];
        }

        newList[elements.length] = element;

        elements = newList;
    }

    /**
     * 出栈 删除掉最后一位元素
     *
     * @return int
     * @author fog
     */
    public int pop() {

        int[] newList = new int[elements.length - 1];

        for (int i = 0; i < elements.length - 1; i++) {
            newList[i] = elements[i];
        }

        elements = newList;

        return elements[elements.length - 1];
    }

    /**
     * 查看栈顶元素
     *
     * @return int
     * @author fog
     */
    public int peek() {
        return elements[elements.length - 1];
    }

    /**
     * 查看栈是否为空
     *
     * @return boolean
     * @author fog
     */
    public boolean isEmpty() {
        return elements.length == 0;
    }

}
